package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
@TableBind(tableName="user_remark_info",pkName = "id")
public class UserRemarkInfo extends BasicModel<UserRemarkInfo> {
    public static final UserRemarkInfo dao = new UserRemarkInfo();
    public static final String SELECT_ALL = "`id`,`create_time`,`account_id`,`user_id`,`device_id`,`remark`";

    public Page<UserRemarkInfo> paginate(Integer pageNumber, Integer pageSize, String column, UserRemarkInfo model) {
        List<Object> paras = new ArrayList<>();
        StringBuilder sqlExceptSelectBuffer = new StringBuilder("FROM " + getTableName() + " WHERE 1=1");
        StringBuilder selectBuffer = new StringBuilder("SELECT ").append(column);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        } else {
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (model != null) {
            String[] attrNames = model.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = model.get(attrName);
                if (attrValue != null) {
                    if("start".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND create_time >= ?");
                    }else if("end".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND create_time <= ?");
                    }else if("remark".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND remark LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else{
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }
        sqlExceptSelectBuffer.append(" ORDER BY `create_time` DESC ");
        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }
            
    public UserRemarkInfo setId(Long id){
        return this.set("id",id);
    }

    public Long getId(){
        return this.get("id");
    }
        
    public UserRemarkInfo setCreateTime(Date createTime){
        return this.set("create_time",createTime);
    }

    public Date getCreateTime(){
        return this.getDate("create_time");
    }
        
    public UserRemarkInfo setAccountId(Long accountId){
        return this.set("account_id",accountId);
    }

    public Long getAccountId(){
        return this.get("account_id");
    }

    public Account getAccount(){
        return Account.dao.findById(getAccountId());
    }
        
    public UserRemarkInfo setUserId(Long userId){
        return this.set("user_id",userId);
    }

    public Long getUserId(){
        return this.get("user_id");
    }

    public User getUser(){
        return User.dao.findById(getUserId());
    }

    public UserRemarkInfo setDeviceId(Long deviceId){
        return this.set("device_id",deviceId);
    }

    public Long getDeviceId(){
        return this.get("device_id");
    }

    public Device getDevice(){
        return Device.dao.findById(getDeviceId());
    }
        
    public UserRemarkInfo setRemark(String remark){
        return this.set("remark",remark);
    }

    public String getRemark(){
        return this.getStr("remark");
    }
}